<template>
  <div class="face-recognition-setting">
    <h3>人脸识别设置</h3>
    <el-form :model="faceForm" label-width="120px" class="setting-form">
      <!-- 基本设置 -->
      <el-card class="setting-card">
        <div slot="header">
          <span>基本设置</span>
        </div>
        <el-form-item label="启用人脸识别">
          <el-switch v-model="faceForm.enabled" />
        </el-form-item>
        <el-form-item label="识别阈值">
          <el-slider v-model="faceForm.threshold" :min="60" :max="100" :step="1" show-input />
          <div class="form-tip">建议设置在80-95之间，数值越大识别越严格</div>
        </el-form-item>
        <el-form-item label="允许重复验证">
          <el-switch v-model="faceForm.allowDuplicate" />
          <div class="form-tip">开启后允许在指定时间内重复进行人脸识别</div>
        </el-form-item>
        <el-form-item label="重复间隔时间" v-if="faceForm.allowDuplicate">
          <el-input-number v-model="faceForm.duplicateInterval" :min="1" :max="60" label="分钟" />
          <span class="unit">分钟</span>
        </el-form-item>
      </el-card>

      <!-- 验证场景 -->
      <el-card class="setting-card">
        <div slot="header">
          <span>验证场景</span>
        </div>
        <el-form-item label="打卡验证">
          <el-checkbox-group v-model="faceForm.verifyScenes">
            <el-checkbox label="clockIn">上班打卡</el-checkbox>
            <el-checkbox label="clockOut">下班打卡</el-checkbox>
            <el-checkbox label="overtime">加班打卡</el-checkbox>
          </el-checkbox-group>
        </el-form-item>
      </el-card>

      <!-- 设备管理 -->
      <el-card class="setting-card">
        <div slot="header">
          <span>设备管理</span>
          <el-button style="float: right" type="primary" size="small" @click="handleAddDevice">添加设备</el-button>
        </div>
        <el-table :data="faceForm.devices" border style="width: 100%">
          <el-table-column prop="name" label="设备名称" width="180">
            <template slot-scope="scope">
              <el-input v-model="scope.row.name" placeholder="请输入设备名称" />
            </template>
          </el-table-column>
          <el-table-column prop="location" label="安装位置" width="180">
            <template slot-scope="scope">
              <el-input v-model="scope.row.location" placeholder="请输入安装位置" />
            </template>
          </el-table-column>
          <el-table-column prop="status" label="状态" width="120">
            <template slot-scope="scope">
              <el-tag :type="scope.row.status === 'online' ? 'success' : 'danger'">
                {{ scope.row.status === 'online' ? '在线' : '离线' }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="150">
            <template slot-scope="scope">
              <el-button type="text" @click="handleEditDevice(scope.row)">编辑</el-button>
              <el-button type="text" class="delete-btn" @click="handleDeleteDevice(scope.$index)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>

      <!-- 保存按钮 -->
      <el-form-item>
        <el-button type="primary" @click="handleSave">保存设置</el-button>
        <el-button @click="handleReset">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: 'FaceRecognitionSetting',
  data() {
    return {
      faceForm: {
        enabled: true,
        threshold: 85,
        allowDuplicate: false,
        duplicateInterval: 5,
        verifyScenes: ['clockIn', 'clockOut'],
        devices: [
          {
            name: '前台设备1',
            location: '公司前台',
            status: 'online'
          },
          {
            name: '后门设备1',
            location: '员工通道',
            status: 'offline'
          }
        ]
      }
    }
  },
  methods: {
    handleAddDevice() {
      this.faceForm.devices.push({
        name: '',
        location: '',
        status: 'offline'
      })
    },
    handleEditDevice(device) {
      // TODO: 实现设备编辑功能
      console.log('编辑设备:', device)
    },
    handleDeleteDevice(index) {
      this.$confirm('确定要删除该设备吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.faceForm.devices.splice(index, 1)
        this.$message.success('设备已删除')
      }).catch(() => {})
    },
    handleSave() {
      // TODO: 调用API保存设置
      this.$message.success('人脸识别设置保存成功')
    },
    handleReset() {
      this.$confirm('确定要重置所有设置吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // TODO: 重置为默认设置
        this.$message.success('设置已重置')
      }).catch(() => {})
    }
  }
}
</script>

<style scoped>
.face-recognition-setting {
  padding: 20px;
}

.setting-form {
  max-width: 1200px;
}

.setting-card {
  margin-bottom: 20px;
}

.el-card ::v-deep .el-card__header {
  padding: 10px 20px;
  font-weight: bold;
}

.form-tip {
  font-size: 12px;
  color: #909399;
  margin-top: 5px;
}

.unit {
  margin-left: 10px;
}

.delete-btn {
  color: #F56C6C;
}

.delete-btn:hover {
  color: #F78989;
}
</style>